#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int MAXN = 1e6 + 10;
const int MOD = 998244353;

string s;
ll q;

char dfs(ll t, ll k) {
    if (t == 0) return s[k];
    if (k == 1) {
        int m = t % 3;
        char temp = s[1] + m;
        if (temp > 'C') temp -= 3;
        return temp;
    }
    ll nk = (k+1)/2;
    char nxt = dfs(t-1, nk);

    if (nxt == 'A') {
        return nk*2 != k ? 'B' : 'C';
    } else if (nxt == 'B') {
        return nk*2 != k ? 'C' : 'A';
    } else {
        return nk*2 != k ? 'A' : 'B';
    }
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin >> s >> q;
    s = "$" + s;
    while(q--) {
        ll t, k;
        cin >> t >> k;
        char res = dfs(t, k);
        cout << res << endl;
    }

    return 0;
}